/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Profissional;
import model.TipoUsuario;
import model.Usuario;
import model.UsuarioDAO;

/**
 *
 * @author rosana
 */
public class TrataCadastroUsuario extends Comando {

    @Override
    public void executar(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, IOException, ServletException {
         response.setContentType("text/html;charset=UTF-8");
         /*inclui  usuario*/
        String profissionalid = request.getParameter("profissional");        
        String tipoid = request.getParameter("tipo");          
        String login = request.getParameter("login");
        String senha = request.getParameter("senha");
        /*inclui  usuario*/
        /*inclui  tipo*/
        String nometipo = request.getParameter("nometipo");        
        String permissaotipo = request.getParameter("permissaotipo");
        /*inclui  tipo*/
        String nomeprof = request.getParameter("nomeprof");
        String cpfprof = request.getParameter("cpfprof");
        String rgprof = request.getParameter("rgprof");
        String datanascprof = request.getParameter("datanascprof");
        String enderecoprof = request.getParameter("enderecoprof");
        String telefoneprof = request.getParameter("telefoneprof");
        String celularprof = request.getParameter("celularprof");
        String emailprof = request.getParameter("emailprof");
        
        
        HttpSession session = request.getSession(false);
        Usuario usuario = (Usuario) session.getAttribute("identifica");
        PrintWriter out = response.getWriter();
        try {
            
            
            Profissional profissional = new Profissional(nomeprof, cpfprof, rgprof, datanascprof, enderecoprof, telefoneprof, celularprof, emailprof);
            profissional.setIdPessoa(Integer.parseInt(profissionalid));
            
            TipoUsuario tipousu = new TipoUsuario(nometipo, permissaotipo);
            tipousu.setIdTipo(Integer.parseInt(tipoid));
            
            Usuario usuario2 = new Usuario(profissional, login, senha, tipousu);           
            
            UsuarioDAO dao = new UsuarioDAO();
            dao.inserir(usuario2);
            
            response.sendRedirect("usuarioCadastrado.jsp");
       } catch(SQLException ex){
            throw new ServletException(ex);
        }catch (ClassNotFoundException cnfe) {
            throw new ServletException(cnfe);
        }
            out.close();
    }

    }
